package cn.virens.common.spring.annotations;

import cn.virens.common.exception.APIException;
import cn.virens.common.utils.Assert;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.annotation.AnnotationAttributes;

import java.lang.reflect.Method;

public class VirAbstractMethodPointcut extends StaticMethodMatcherPointcut implements InitializingBean {
    protected VirAbstractAnnotationSource mVirAbstractAnnotationSource;

    public void setAnnotationSource(VirAbstractAnnotationSource mVirAbstractAnnotationSource) {
        this.mVirAbstractAnnotationSource = mVirAbstractAnnotationSource;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.isNull(mVirAbstractAnnotationSource);
    }

    @Override
    public boolean matches(Method method, Class<?> targetClass) throws APIException {
        return findAnnotationAttributes(method, targetClass) != null;
    }

    protected AnnotationAttributes findAnnotationAttributes(Method method, Class<?> targetClass) {
        return mVirAbstractAnnotationSource.findAnnotationAttributes(method, targetClass);
    }
}
